package com.uploadtest.jiaotizhixing;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class LockConditionTest {
    public static void main(String[] args) {
        final char arrayA[] = "1234567".toCharArray();
        final char arrayB[] = "ABCDEFG".toCharArray();
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Runnable runnable = () -> {
            try {
                lock.lock();
                for (char charA : arrayA) {
                    System.out.print(charA);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
 
        };
        Runnable runnable1 = () -> {
            try {
                lock.lock();
                for (char charB : arrayB) {
                    System.out.print(charB);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        };
        new Thread(runnable).start();
        new Thread(runnable1).start();
    }
}